﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI
{
    public abstract class Layout
    {
        public List<PartFrame> Frames { get; set; }

        public Layout()
        {
            Frames = new List<PartFrame>();
        }

        public abstract void AttachToPart(ILayoutContainer part);

        [Obsolete("以后frame也将成为组件，次方法将被AddPart()替代")]
        public void AddFrame(string position, PartFrame frame)
        {
            Frames.Add(frame);
            OnAddFrame(position, frame);
        }

        /// <summary>
        /// 往布局中添加组件
        /// </summary>
        /// <param name="position"></param>
        /// <param name="part"></param>
        public abstract void AddPart(Point position, Part part);

        protected abstract void OnAddFrame(string position, PartFrame frame);

        /// <summary>
        /// 获取ui控件内容
        /// </summary>
        /// <returns></returns>
        public abstract object GetUIContent();

        /// <summary>
        /// 将组件加入到布局中
        /// </summary>
        /// <param name="frameName"></param>
        /// <param name="part"></param>
        public void AddPart(string frameName, Part part)
        {
            for (int index = 0; index < Frames.Count; index++)
            {
                PartFrame frame = Frames[index];
                if (frame.Name == frameName)
                {
                    frame.ShowPart(part, SwitchAnimationFactory.Create("None"));
                    return;
                }
                else if(frame.Layout !=null)
                {
                    AddPart(frame, frameName, part);
                }
            }
        }

        private void AddPart(PartFrame parentFrame, string frameName, Part part)
        {
            for (int index = 0; index < parentFrame.Layout.Frames.Count; index++)
            {
                PartFrame frame = parentFrame.Layout.Frames[index];
                if (frame.Name == frameName)
                {
                    frame.ShowPart(part, SwitchAnimationFactory.Create("None"));
                    return;
                }
                else if (frame.Layout != null)
                {
                    AddPart(frame, frameName, part);
                }
            }
        }

        /// <summary>
        /// 获取组件
        /// </summary>
        /// <param name="partName"></param>
        /// <returns></returns>
        public UI.Part GetPart(string partName)
        {
            UI.Part bingoPart = null;
            for (int index = 0; index < Frames.Count; index++)
            {
                PartFrame frame = Frames[index];
                if (frame.CurrentPart != null && frame.CurrentPart.Name == partName)
                {
                    bingoPart = frame.CurrentPart;
                    break;
                }

                bingoPart = GetPart(frame, partName);

                if (bingoPart != null)
                {
                    break;
                }
            }
            return bingoPart;
        }

        private UI.Part GetPart(PartFrame parentFrame, string partName)
        {
            if (parentFrame.Layout != null)
            {
                return parentFrame.Layout.GetPart(partName);
            }
            return null;
        }
    }
}
